package com.example.byteinputstream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreamDemo7 {
    public static void main(String[] args) {
        //创建对象
        //定义
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //初始化
            fis = new FileInputStream("src/com/example/file/a.txt");
            fos = new FileOutputStream("src/com/example/file/b.txt");
            //拷贝文件
            byte[] buf = new byte[1024];
            int read = fis.read(buf);
            while (read != -1) {
                fos.write(buf, 0, read);
                read = fis.read(buf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
